app.factory('commonFactory', ["$http", '$rootScope', '$location', '$window', '$compile', '$timeout', '$interval', '$q', function ($http, $rootScope, $location, $window, $compile, $timeout, $interval, $q) {
    var domain = window.location.protocol+'//'+window.location.host
    var publicmethod = {
        // baseurl: "http://192.168.0.109:9084",//测试
        baseurl: "http://192.168.0.109:9004",//测试
        baseurlstatistic: "http://192.168.0.109:9012",//红包和体验金到账接口地址
        pointurl: "http://192.168.0.109:9006",//定点标的测试接口
        copyurl: "http://192.168.0.109:3018/product/",//复制链接
        timeurl: "http://192.168.0.109:9005",//获取服务器时间接口
        // baseurl: domain,//正式
        // baseurlstatistic: window.location.protocol+"//static.youlijinfu.com",//红包和体验金到账线上接口地址
        // pointurl: "https://assign.youlijinfu.com",//定点标的正式接口
        // copyurl :"https://assign.youlijinfu.com/product/",//复制链接正式        
        // timeurl: "https://api.youlijinfu.com",//获取服务器时间接口
        ajax: function (url, data, callback, err, method) {
            if (typeof callback != "function") {
                callback = data;
                data = {};
            }
            var u_id = localStorage.getItem('u_id');
            data.u_id = u_id;
            if (!url.startWith("http")) {
                url = publicmethod.baseurl + url;
            }

            if (method == "post") {
                var d = "";
                angular.forEach(data, function (value, key) {
                    if (d) {
                        d = d + "&";
                    }
                    d = d + key + "=" + value;
                })
                $http({
                    url: url,
                    method: "POST",
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    data: d,
                    timeout: 60 * 1000,
                }).success(callback).error(function () {
                    if (typeof err == "function") {
                        err({ code: -1, data: '请求失败，请稍后再试' });
                    }
                });
            } else {
                $http.get(url, { params: data }).success(callback).error(function () {
                    if (typeof err == "function") {
                        err({ code: -1, data: '请求失败，请稍后再试' });
                    }
                });
            }
        },
        go: function (path) {
            if (path === 'back') {
                var back, curIndex = -1;
                angular.forEach($rootScope.backs, function (item, i) {
                    if (item.path == $location.$$path.toLowerCase()) {
                        $rootScope.Template.backlink = item.backurl;
                        return;
                    }
                });

                angular.forEach($rootScope.backs, function (item, i) {
                    if (item.path == $location.$$path.toLowerCase()) {
                        curIndex = i;
                        return;
                    }
                });
                if (curIndex > -1) {
                    $rootScope.backs.splice(curIndex, 1);
                }
                if ($rootScope.Template.backlink) {
                    $location.url($rootScope.Template.backlink);
                } else {
                    $window.history.back();
                }
            }
            else { // Go to the specified path
                $location.url(path);
            }
        },
        sub: function (arg1, arg2) {//浮点数减法
            var r1, r2, m, n;
            try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
            try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
            m = Math.pow(10, Math.max(r1, r2));
            //动态控制精度长度
            n = (r1 >= r2) ? r1 : r2;
            return ((arg1 * m - arg2 * m) / m).toFixed(n);
        },
        subc: function (arg1, arg2) {//浮点数乘法
            var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
            try { m += s1.split(".")[1].length } catch (e) { }
            try { m += s2.split(".")[1].length } catch (e) { }
            return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
        },
        getResult: function () {
            return this.result;
        },
        setResult: function (res) {
            this.result = res;
        },
        dateFormat: function (date) {
            date.getFull
            return
        },
        DateFormat: function (fmt, type) {
            fmt = new Date(fmt);
            var Y = fmt.getFullYear(),
                M = fmt.getMonth() + 1, //月份 
                D = fmt.getDate(), //日 
                h = fmt.getHours(), //小时 
                m = fmt.getMinutes(), //分 
                s = fmt.getSeconds() //秒  
            if (M < 10) {
                M = "0" + M
            }
            if (D < 10) {
                D = "0" + D
            }
            if (h < 10) {
                h = "0" + h
            }
            if (m < 10) {
                m = "0" + m
            }
            if (s < 10) {
                s = "0" + s
            }
            fmt = Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s;
            if (type == 1) {
                fmt = Y + "-" + M + "-" + D;
            }
            return fmt;
        },
        loading: function (t, time) {
            var html = '';
            html += '<div class="load_shadow">';
            html += '<div class="load_pos">';
            html += '<img src="img/loading.gif" />';
            html += '</div>';
            html += '</div>';
            if (t) {
                    $(document.body).css('overflow', 'hidden');
                    $(document.body).prepend(html)
                    $('.load_pos').css('margin-top', $(document.body).height() / 2 - 80)
            } else {
                if ($('.load_shadow')) {
                    $(document.body).css('overflow', 'auto');
                    $('.load_shadow').remove();
                }
            }
        }

    }
    return publicmethod;
}]);
function HttpInterceptor($q, $location) {
    return {
        response: function (res) {
            if (res.data.Code == 8) {
                $location.path('/login')
            }
            return res;
        }
    };
}